﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Text;
using CinePlancito.Dominio;

namespace CinePlancito.Controllers
{
    public class PeliculaController : Controller
    {
        string RestPeliculas = "http://localhost:15497/Peliculas.svc/Peliculas";
        JavaScriptSerializer js = new JavaScriptSerializer();


        public ActionResult ListaPeliculas()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest
                .Create(RestPeliculas + "/");
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string peliculaJson = reader.ReadToEnd();
            List<Pelicula> peliculas = js.Deserialize<List<Pelicula>>(peliculaJson);
            return View(peliculas);
        }
        public ActionResult DetallesPelicula(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestPeliculas + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string peliculaJson = reader.ReadToEnd();
            Pelicula pelicula = js.Deserialize<Pelicula>(peliculaJson);
            return View(pelicula);
        }

        public ActionResult EditarPelicula(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestPeliculas + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string peliculaJson = reader.ReadToEnd();
            Pelicula pelicula = js.Deserialize<Pelicula>(peliculaJson);
            return View(pelicula);
        }

        [HttpPost]
        public ActionResult EditarPelicula(string id, FormCollection collection)
        {
            try
            {
                string nombre = (string)collection["Nombre"];
                string descripcion = (string)collection["Descripcion"];
                string periodo = (string)collection["Periodo"];

                const string quote = "\"";
                string postdata = "{"
                                + quote + "Idpelicula" + quote + ":" + quote + id + quote + ","
                                + quote + "Nombre" + quote + ":" + quote + nombre + quote + ","
                                + quote + "Descripcion" + quote + ":" + quote + descripcion + quote + ","
                                + quote + "Periodo" + quote + ":" + quote + periodo + quote +
                                  "}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestPeliculas);
                req.Method = "PUT";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("ListaPeliculas");
            }
            catch
            {


                return View();
            }
        }

        public ActionResult RegistrarPelicula()
        {
            return View();
        }
        //CREAR COMBO CON REST
        [HttpPost]
        public ActionResult RegistrarPelicula(FormCollection collection)
        {




            try
            {


                if (collection["Idpelicula"].Trim().Length == 0) ModelState.AddModelError("Idpelicula", "El ID Debe ser ingresado.");
                if (collection["Nombre"].Trim().Length == 0) ModelState.AddModelError("Nombre", "Debe Ingresar Nombre");
                if (collection["Descripcion"].Trim().Length == 0) ModelState.AddModelError("Descripcion", "Debe Ingresar Descripción");
                if (collection["Periodo"].Trim().Length == 0) ModelState.AddModelError("Periodo", "Debe Ingresar Periodo");




                if (!ModelState.IsValid)
                {
                    Pelicula pelicula = null;
                    pelicula = new Pelicula()
                    {
                        Idpelicula = int.Parse(collection["Idpelicula"]),
                        Nombre = (string)collection["Nombre"],
                        Descripcion = (string)collection["Descripcion"],
                        Periodo = (string)collection["Periodo"]
                    };

                    return View(pelicula);

                }



                int idpelicula = int.Parse(collection["Idpelicula"]);
                string nombre = (string)collection["Nombre"];
                string descripcion = (string)collection["Descripcion"];
                string periodo = (string)collection["Periodo"];





                const string quote = "\"";
                string postdata = "{"
                                + quote + "Idpelicula" + quote + ":" + quote + idpelicula + quote + ","
                                + quote + "Nombre" + quote + ":" + quote + nombre + quote + ","
                                + quote + "Descripcion" + quote + ":" + quote + descripcion + quote + ","
                                + quote + "Periodo" + quote + ":" + quote + periodo + quote +
                                  "}";

                // CREAR REGISTRO 
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestPeliculas);
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("ListaPeliculas");
            }
            catch
            {

                return View();
            }
        }
        public ActionResult EliminarPelicula(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestPeliculas + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string peliculaJson = reader.ReadToEnd();
            Pelicula pelicula = js.Deserialize<Pelicula>(peliculaJson);
            return View(pelicula);
        }
        [HttpPost]
        public ActionResult EliminarPelicula(string id, FormCollection collection)
        {
            try
            {

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestPeliculas + "/" + id);
                req.Method = "DELETE";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string peliculaJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                Combo peliculaEliminado = js.Deserialize<Combo>(peliculaJson);
                return RedirectToAction("ListaPeliculas");
            }
            catch
            {
                return View();
            }
        }


    }
}